package table_data;


import beans.AreaCode;
import common.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class TransAreaCodeKbseToLocal {
    public static void main(String[] args) {
        try {
            File file=new File("C:\\Users\\ts647\\Desktop\\output_area_code");//文件不存在则创建
            FileOutputStream fos=new FileOutputStream(file,true);//第二个参数为true代表追加，为false(默认)为覆盖
            Class.forName(Utils.driver);//加载KBase驱动
            Connection conn = DriverManager.getConnection(Utils.area_code_url, Utils.user, Utils. password_174);// 获取连接
            Statement statement = conn.createStatement();//得到声明
            //当kbase某列值为null（看起来就是空）时，该列也存在，例子："unit_type_code":""
            ResultSet rs = statement.executeQuery("SELECT * FROM CYFD_CLS2_NEW");//执行SQL语句返回结果集
            while (rs.next()) { //循环输出结果集
                String area_num = rs.getString("SYS_FLD_CLASS_CODE_NEW");
                String area_code = rs.getString("SYS_FLD_CLASS_CODE");
                String show_name = rs.getString("SYS_FLD_CLASS_NAME_NEW");
                AreaCode ac= new AreaCode(area_num,area_code,show_name);
                fos.write(ac.toString().getBytes());
            }
            fos.close();
            rs.close();//关闭结果集
            conn.close();//关闭数据库连接
        } catch (Exception e) {
            System.out.print("KBase ERROR:" + e.getMessage());
        }

    }
}
